//
//  ReadTableViewCell.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/23.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "ReadTableViewCell.h"
@implementation ReadTableViewCell

- (void)dealloc {
    
    [_label release];
    [_question release];
    [_writer release];
    [_detail release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.label = [YHYBaseLabel new];
        _label.textColor = [UIColor blueColor];
        _label.font = [UIFont systemFontOfSize:15];
        _label.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_label];
        
        self.question = [YHYBaseLabel new];
        _question.font = [UIFont systemFontOfSize:17];
        _question.textColor = [UIColor grayColor];
        [self.contentView addSubview: _question];
        
        self.writer = [YHYBaseLabel new];
        _writer.font = [UIFont systemFontOfSize:13];
        _writer.textColor = [UIColor blueColor];
        [self.contentView addSubview:_writer];
        
        self.detail = [YHYBaseLabel new];
        _detail.textColor = [UIColor lightGrayColor];
        self.detail.numberOfLines = 0;
        [self.contentView addSubview:_detail];
        
        self.smallView = [UIView new];
        _smallView.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_smallView];
        
//        _question.backgroundColor = [UIColor cyanColor];
//        _writer.backgroundColor = [UIColor cyanColor];
//        _detail.backgroundColor = [UIColor cyanColor];
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(5);
        make.right.equalTo(self.contentView).offset(-10);
        make.width.equalTo(@50);
        make.height.equalTo(@20);
        
    }];

    _label.layer.cornerRadius = 5;
    _label.layer.masksToBounds = YES;
    _label.layer.borderColor = [UIColor blueColor].CGColor;
    _label.layer.borderWidth = 2;

    
    [_question mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(5);
        make.left.equalTo(self.contentView).offset(10);
        make.width.equalTo(@350);
        make.height.equalTo(@25);
        
    }];
    
    [_writer mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_question.mas_bottom).offset(5);
        make.left.equalTo(self.contentView).offset(10);
        make.height.equalTo(@15);
        make.width.equalTo(@200);
        
    }];
    
    _textHeight = [[self class]heightWithText:self.detail.text];
    [_detail mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_writer.mas_bottom).offset(5);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(@(-10));
        make.height.equalTo(@(_textHeight));
        
    }];
    
    [_smallView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.contentView).offset(0);
        make.left.equalTo(self.contentView).offset(10);
        make.width.equalTo(@(INWIDTH - 20));
        make.height.equalTo(@1);
        
    }];
    
    // 刷新
    [self layoutIfNeeded];
}

+ (CGFloat)heightWithText:(NSString *)text {
    if (text == nil) {
        return 0;
    }
    
    NSDictionary *fontDic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    CGSize size = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 90, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil].size;
    
    return size.height + 50;
    
}

- (void)setEssayModel:(EssayModel *)essayModel {
    if (_essayModel != essayModel) {
        [_essayModel release];
        _essayModel = [essayModel retain];
    }
    
    _question.text = essayModel.hp_title;
    AutoModel *autoModel = essayModel.author[0];
    _writer.text = autoModel.user_name;
    _detail.text = essayModel.guide_word;
}

- (void)setSerialModel:(SerialModel *)serialModel {
    if (_serialModel != serialModel) {
        [_serialModel release];
        [_serialModel retain];
    }
    
    _question.text = serialModel.title;
    _writer.text = serialModel.authorNew.user_name;
    _detail.text = serialModel.excerpt;
}

-(void)setQuestionModel:(QuestionModel *)questionModel {
    if (_questionModel != questionModel) {
        [_questionModel release];
        _questionModel = [questionModel retain];
    }
    
    _question.text = questionModel.question_title;
    _writer.text = questionModel.answer_title;
    _detail.text = questionModel.answer_content;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
